package lomasky.xui;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.Date;


/**
 * Created by ma on 2015/7/10.
 * RecyclerView通用ViewHolder
 */
public class BaseViewHolderForListView   {
    private SparseArray<View> sparseArray;
    private View itemView;
    private int mPosition;
    private Context context;
    public BaseViewHolderForListView(Context context, ViewGroup parent, int layoutId, int position) {
        this.context = context;
        this.mPosition = position;

        itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        itemView.setTag(this);
        this.sparseArray = new SparseArray<View>();

    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static BaseViewHolderForListView get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
        if (convertView == null) {
            return new BaseViewHolderForListView(context, parent, layoutId, position);
        }
        return (BaseViewHolderForListView) convertView.getTag();
    }
    public <T extends View> T getView(int id) {
        View childView = sparseArray.get(id);
        if (childView == null) {
            childView = itemView.findViewById(id);
            sparseArray.put(id, childView);
        }
        return (T) childView;
    }

    public View getConvertView() {
        return itemView;
    }

    public TextView getTextView(int viewId) {
        return getView(viewId);
    }


    public RecyclerView getRecyclerView(int viewId) {
        return getView(viewId);
    }

    public CheckBox getCheckBox(int viewId) {
        return getView(viewId);
    }

    public Button getButton(int viewId) {
        return getView(viewId);
    }


    public ImageView getImageView(int viewId) {
        return getView(viewId);
    }


    public BaseViewHolderForListView setText(int viewId, String value) {
        getTextView(viewId).setText(value == null ? "" : value);
        return BaseViewHolderForListView.this;

    }

    public BaseViewHolderForListView setText(int viewId, int value) {
        getTextView(viewId).setText(value + "");
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setText(int viewId, float value) {
        getTextView(viewId).setText(value + "");
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setText(int viewId, double value) {
        getTextView(viewId).setText(value + "");
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setText(int viewId, long value) {
        getTextView(viewId).setText(value + "");
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setText(int viewId, Date value) {
        getTextView(viewId).setText(value == null ? "" : value.toString());
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setChecked(int viewId, boolean value) {
        getCheckBox(viewId).setChecked(value);
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setChecked(int viewId, boolean value, String text) {
        getCheckBox(viewId).setChecked(value);
        getCheckBox(viewId).setText(text);
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setBackgroundColor(int viewId, int color) {
        View view = getView(viewId);
        view.setBackgroundColor(color);
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setBackgroundResource(int viewId, int backgroundRes) {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundRes);
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setVisible(int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setOnTouchListener(int viewId, View.OnTouchListener listener) {
        View view = getView(viewId);
        view.setOnTouchListener(listener);
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setOnLongClickListener(int viewId, View.OnLongClickListener listener) {
        View view = getView(viewId);
        view.setOnLongClickListener(listener);
        return BaseViewHolderForListView.this;
    }

    public BaseViewHolderForListView setTag(int viewId, Object tag) {
        View view = getView(viewId);
        view.setTag(tag);
        return BaseViewHolderForListView.this;
    }


    public BaseViewHolderForListView setImage(int viewId, File file) {

        if (file != null) {
            Glide.with(itemView.getContext())
                    .load(file)
                    .placeholder(R.drawable.pictures_no)
                    .error(R.drawable.pictures_no)
                    .override(300, 300).centerCrop()
                    .into(getImageView(viewId));
        }
        return BaseViewHolderForListView.this;

    }

    public BaseViewHolderForListView setImage(int viewId, String url) {


        if (!TextUtils.isEmpty(url)) {
            Glide.with(itemView.getContext())
                    .load(url)
                    .placeholder(R.drawable.pictures_no)
                    .error(R.drawable.pictures_no)
                    .override(300, 300)
                    .centerCrop()
                    .into(getImageView(viewId));


        }
        return BaseViewHolderForListView.this;

    }

    public BaseViewHolderForListView setImage(int viewId, int drawableId) {
        Glide.with(itemView.getContext())
                .load(drawableId)
                .into(getImageView(viewId));
        return BaseViewHolderForListView.this;
    }


}